package com.it.gw.modules.user.activity

import android.content.Intent
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import com.it.common.utils.CommonRxTimerTask
import com.it.common.utils.EncryptUtil
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityLoginBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.user.viewmodel.LoginViewModel


/**
 * 登录界面
 */
class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    private val loginViewModel by viewModels<LoginViewModel>()

    companion object {
        const val TAG = "LoginActivity"
    }

    override fun initObserver() {
        loginViewModel.sendCodeLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "获取验证码成功", Toast.LENGTH_SHORT).show()
                startTimeTask()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }

        }
        loginViewModel.loginLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }
            binding.tvOk.text = "登录中"
            binding.tvOk.isEnabled = true
        }

    }

    var loginTypePwd = false//true 切换密码登录 false 验证码登录
    override fun initListener() {

        binding.ivEyes.setOnClickListener {
            if (isShowPwd) {
                binding.etPwd.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            } else {
                binding.etPwd.setInputType(InputType.TYPE_CLASS_TEXT)

            }
            isShowPwd = !isShowPwd

        }
        binding.llChange.setOnClickListener {
            loginTypePwd = !loginTypePwd
            setLoginTypeText()
        }
        binding.etUserName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                setBtnEnable()
            }

        })
        binding.tvCode.setOnClickListener {
            if (!EncryptUtil.isValidPhoneNumber(binding.etUserName.text.toString())) {
                Toast.makeText(this@LoginActivity, "请输入正确的手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            ProgressDialog.show(this)
            loginViewModel.getCode((binding.etUserName.text.toString()))


        }
        binding.rlOk.setOnClickListener {
            if (binding.etUserName.text.toString().isNullOrEmpty()) {
                Toast.makeText(this@LoginActivity, "请输入账号或手机号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (loginTypePwd) {
                if (binding.etPwd.text.toString().isNullOrEmpty()) {
                    Toast.makeText(this@LoginActivity, "请输入密码", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
            } else {
                if (binding.etCode.text.toString().isNullOrEmpty()) {
                    Toast.makeText(this@LoginActivity, "请输入验证码", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
            }

            binding.tvOk.text = "登录中..."
            binding.tvOk.isEnabled = false
//            loginViewModel.login(loginName = "ll", password = "qwer@.123")

            Log.d(TAG, "initView: ${binding.etPwd.text}")
            loginViewModel.login(
                loginName = binding.etUserName.text.toString(),
                password = binding.etPwd.text.toString(),
                flag = if (loginTypePwd) "1" else "2",
                phone = binding.etUserName.text.toString(),
                verifyCode = binding.etCode.text.toString()
            )

        }


    }

    private fun setBtnEnable() {
        if (TextUtils.isEmpty(binding.etUserName.text.toString()) || isGetCode) {
            binding.tvCode.isEnabled = false
            binding.tvCode.setBackgroundResource(R.drawable.shape_btn_dark_4)
        } else {
            binding.tvCode.isEnabled = true
            binding.tvCode.setBackgroundResource(R.drawable.shape_btn_4)
        }
    }

    var isGetCode = false;
    val codeTask = CommonRxTimerTask()
    var time = 30;
    private fun startTimeTask() {
        if (isGetCode) {
            return
        }
        isGetCode = !isGetCode
        setBtnEnable()
        binding.tvCode.setText("${time}")
        codeTask.interval(1000) {
            time = time - 1;
            binding.tvCode.setText("${time}")
            if (time == 0) {
                clearCode();
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        codeTask.cancel()
    }

    private fun setLoginTypeText() {
        if (loginTypePwd == true) {

            binding.tvChange.text = "切换验证码登录"
            binding.rlPwd.visibility = View.VISIBLE
            binding.rlCode.visibility = View.GONE
            clearCode()
            setBtnEnable()


        } else {
            binding.tvChange.text = "切换密码登录"

            binding.rlPwd.visibility = View.GONE
            binding.rlCode.visibility = View.VISIBLE



        }
    }

    private fun clearCode() {
        codeTask.cancel()
        binding.tvCode.setText("获取验证码")
        isGetCode = false
        time = 30
        setBtnEnable()
    }

    var isShowPwd = false

    override fun initData() {

    }

    override fun initView() {
        setLoginTypeText()

    }

    override fun setContentView() {
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}